﻿using System;
using System.Text.RegularExpressions;
using System.Web;
using System.Data;
using QunYing.Model;

namespace QunYing.Web.market
{
    public class ShopCart
    {
        public static bool AddGoods(Model.GoodsInfo model, string type)
        {
            DataTable dt = null;
            if (HttpContext.Current.Session["ShopCart"] == null)
            {
                dt = new DataTable();
                dt.Columns.Add("商品ID", typeof(int));
                dt.Columns.Add("商品名称", typeof(string));
                dt.Columns.Add("品牌", typeof (string));
                dt.Columns.Add("单位", typeof(string));
                dt.Columns.Add("单价", typeof(decimal));
                dt.Columns.Add("赠送积分", typeof(int));
                dt.Columns.Add("消耗积分", typeof(int));
                dt.Columns.Add("数量", typeof(int));
                dt.Columns.Add("类型", typeof (string));
            }
            else
            {
                dt = HttpContext.Current.Session["ShopCart"] as DataTable;
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    //如果读到该商品的ID 则该商品己经存在 则将该商品的数量加1  
                    if (dt.Rows[j]["商品ID"].ToString() == model.商品ID.ToString() && dt.Rows[j]["类型"].ToString() == type)
                    {
                        //如果商品 己经存在 则购买数量加上1  
                        dt.Rows[j]["数量"] = Convert.ToInt32(dt.Rows[j]["数量"]) + 1;
                        //Session保存购物车  
                        HttpContext.Current.Session["ShopCart"] = dt;
                        return true;
                    }
                }
            }

            //如果该商品信息不存在 购物车中 则添加一个新的商品信息  
            DataRow dr = dt.NewRow();
            dr["商品ID"] = model.商品ID;
            dr["商品名称"] = model.商品名称;
            dr["品牌"] = model.商品品牌;
            dr["单位"] = model.单位;
            switch (type)
            {
                case Enums.Hot:
                    dr["单价"] = model.原价;
                    dr["赠送积分"] = model.赠送积分;
                    dr["消耗积分"] = 0;
                    break;
                case Enums.Sale:
                    dr["单价"] = model.现价;
                    dr["赠送积分"] = model.赠送积分;
                    dr["消耗积分"] = 0;
                    break;
                case Enums.Score:
                    dr["单价"] = 0;
                    dr["赠送积分"] = 0;
                    dr["消耗积分"] = model.兑换积分;
                    break;
                default:
                    dr["单价"] = model.原价;
                    dr["赠送积分"] = model.赠送积分;
                    dr["消耗积分"] =0;
                    break;
            }
            dr["数量"] = 1;
            dr["类型"] = type;
            dt.Rows.Add(dr);
            HttpContext.Current.Session["ShopCart"] = dt;
            return true;
        }

        public static bool DelGoods(string id, string type)
        {
            DataTable dt = HttpContext.Current.Session["ShopCart"] as DataTable;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (dt.Rows[j]["商品ID"].ToString() == id && dt.Rows[j]["类型"].ToString() == type)
                { 
                    dt.Rows.RemoveAt(j);
                    HttpContext.Current.Session["ShopCart"] = dt;
                    return true;
                }
            }
            return false;
        }
        public static bool UpdateGoods(string id,int num, string type)
        {
            DataTable dt = HttpContext.Current.Session["ShopCart"] as DataTable;
            for (int j = 0; j < dt.Rows.Count; j++)
            {
                if (dt.Rows[j]["商品ID"].ToString() == id && dt.Rows[j]["类型"].ToString() == type)
                {
                    dt.Rows[j]["数量"] = num;
                    HttpContext.Current.Session["ShopCart"] = dt;
                    return true;
                }
            }
            return false;
        }
    }
}
